import 'package:json_annotation/json_annotation.dart';
import 'song.dart';

part 'playlist.g.dart';

@JsonSerializable()
class Playlist {
  final String id;
  final String name;
  final String description;
  final String coverUrl;
  final DateTime createdAt;
  final DateTime updatedAt;
  final List<Song> songs;
  final bool isDefault; // 是否为系统默认播放列表（如收藏、最近播放）

  const Playlist({
    required this.id,
    required this.name,
    this.description = '',
    this.coverUrl = '',
    required this.createdAt,
    required this.updatedAt,
    this.songs = const [],
    this.isDefault = false,
  });

  // 创建收藏播放列表
  factory Playlist.favorites() {
    final now = DateTime.now();
    return Playlist(
      id: 'favorites',
      name: '我的收藏',
      description: '收藏的歌曲',
      isDefault: true,
      createdAt: now,
      updatedAt: now,
    );
  }

  // 创建最近播放列表
  factory Playlist.recentPlayed() {
    final now = DateTime.now();
    return Playlist(
      id: 'recent_played',
      name: '最近播放',
      description: '最近播放的歌曲',
      isDefault: true,
      createdAt: now,
      updatedAt: now,
    );
  }

  // 创建我喜欢的音乐
  factory Playlist.liked() {
    final now = DateTime.now();
    return Playlist(
      id: 'liked',
      name: '我喜欢的音乐',
      description: '点赞的歌曲',
      isDefault: true,
      createdAt: now,
      updatedAt: now,
    );
  }

  // 获取播放列表长度
  int get length => songs.length;

  // 获取总时长
  Duration get totalDuration {
    int totalSeconds = 0;
    for (final song in songs) {
      final parts = song.duration.split(':');
      if (parts.length == 2) {
        totalSeconds += int.parse(parts[0]) * 60 + int.parse(parts[1]);
      }
    }
    return Duration(seconds: totalSeconds);
  }

  // 格式化总时长
  String get formattedDuration {
    final duration = totalDuration;
    final hours = duration.inHours;
    final minutes = duration.inMinutes.remainder(60);
    
    if (hours > 0) {
      return '${hours}小时${minutes}分钟';
    } else {
      return '${minutes}分钟';
    }
  }

  // 复制并更新
  Playlist copyWith({
    String? id,
    String? name,
    String? description,
    String? coverUrl,
    DateTime? createdAt,
    DateTime? updatedAt,
    List<Song>? songs,
    bool? isDefault,
  }) {
    return Playlist(
      id: id ?? this.id,
      name: name ?? this.name,
      description: description ?? this.description,
      coverUrl: coverUrl ?? this.coverUrl,
      createdAt: createdAt ?? this.createdAt,
      updatedAt: updatedAt ?? this.updatedAt,
      songs: songs ?? this.songs,
      isDefault: isDefault ?? this.isDefault,
    );
  }

  // 添加歌曲
  Playlist addSong(Song song) {
    final newSongs = List<Song>.from(songs);
    if (!newSongs.any((s) => s.id == song.id)) {
      newSongs.add(song);
    }
    return copyWith(
      songs: newSongs,
      updatedAt: DateTime.now(),
    );
  }

  // 移除歌曲
  Playlist removeSong(String songId) {
    final newSongs = songs.where((song) => song.id != songId).toList();
    return copyWith(
      songs: newSongs,
      updatedAt: DateTime.now(),
    );
  }

  // 是否包含某首歌
  bool containsSong(String songId) {
    return songs.any((song) => song.id == songId);
  }

  // JSON序列化
  factory Playlist.fromJson(Map<String, dynamic> json) => _$PlaylistFromJson(json);
  Map<String, dynamic> toJson() => _$PlaylistToJson(this);

  @override
  bool operator ==(Object other) =>
      identical(this, other) ||
      other is Playlist && runtimeType == other.runtimeType && id == other.id;

  @override
  int get hashCode => id.hashCode;

  @override
  String toString() {
    return 'Playlist{id: $id, name: $name, songsCount: ${songs.length}}';
  }
} 